package org.jeecg.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.entity.DiyProfessorInfo;
import org.jeecg.modules.entity.DiyTableStatus;
import org.jeecg.modules.mapper.DiyTableStatusMapper;
import org.jeecg.modules.service.IDiyProfessorInfoService;
import org.jeecg.modules.service.IDiyTableStatusService;
import org.jeecg.modules.util.ProfessorStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: diy_table_status
 * @Author: jeecg-boot
 * @Date:   2020-05-25
 * @Version: V1.0
 */
@Service
public class DiyTableStatusServiceImpl extends ServiceImpl<DiyTableStatusMapper, DiyTableStatus> implements IDiyTableStatusService {

    @Autowired
    private DiyTableStatusMapper diyTableStatusMapper;

    @Autowired
    ISysBaseAPI iSysBaseAPI;

    @Autowired
    IDiyProfessorInfoService diyProfessorInfoService;
    @Override
    public Page<DiyTableStatus> queryPage(Page<DiyTableStatus> page, DiyTableStatus diyTableStatus) {

        List<DiyTableStatus> diyTableStatuses = diyTableStatusMapper.queryPage(page, diyTableStatus);

        for (DiyTableStatus d :
                diyTableStatuses) {
            // 获得专家申报类型，并转为数组
            String[] ids = d.getProfessorInfo().getProfessorDeclareCategory().split(",");
            // 初始化一个names，没遍历一条数据初始化一次
            String names = "";
            int i = 1;
            for (String id :
                    ids) {
                // 每一个id都是一个申报类型的代号，获取类型名字
                String name = diyProfessorInfoService.getDeclareCategoryName(id);
                names += name;
                i++;
                //如果不是最后一位，都分隔开，
                if(i<=ids.length){
                    names = names +"，";
                }else{
                    // 所有的名字都找见了，放入属性中
                    d.getProfessorInfo().setProfessorDeclareCategory(names);
                    System.out.println(d);
                }
            }
        }
        return page.setRecords(diyTableStatuses);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean check(String professorTableId, boolean check) {
        // 开始修改diy_table_status中的table_status的值
        UpdateWrapper<DiyTableStatus> wrapper = new UpdateWrapper<>();
        // 设置条件
        wrapper.set("table_status",check==true?ProfessorStatus.ACCEPT.getStatus():ProfessorStatus.REFUSE.getStatus())
                .eq("professor_table_id",professorTableId);
        boolean update = this.update(null, wrapper);
        // 开始修改diy_professor_info表中的status的值
        UpdateWrapper<DiyProfessorInfo> wrapper1 = new UpdateWrapper<>();
        // 设置条件
        wrapper1.set("status",check==true?ProfessorStatus.ACCEPT.getStatus():ProfessorStatus.REFUSE.getStatus())
                .eq("id",professorTableId);
        boolean update1 = diyProfessorInfoService.update(null, wrapper1);

        // System.out.println(update&&update1);

        return update&&update1;
    }
}
